/**
 * 
 */
package com.ramon.ohcda.util;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

/**
 * @author Ramon
 */
public class DataHandler {

    private static Date now = Calendar.getInstance().getTime();;

    private static SimpleDateFormat formatter = new SimpleDateFormat(
            "yyyyMMddHHmmss");

    public static void write(HashMap hm) throws IOException {

        String filename = "";
        filename = formatter.format(now);
        filename = "data-" + filename + ".txt";
        FileWriter fw = new FileWriter(new File("datas/" + filename));
        for (Object url : hm.keySet().toArray()) {
            fw.write(url + " " + hm.get(url) + "\r\n");
        }
        fw.close();
    }

    public static HashMap read(File f) throws IOException {
        HashMap hm = new HashMap();
        String[] line;
        DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));
        while(dis.available()!=0) {
            line=dis.readLine().split(" ");
            hm.put(line[0], line[1]);
        }     
        dis.close();
        return hm;
    }
}
